//
//  NewsTableCell.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/20.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

/*
 * 默认cell类型，有一张图片，标题，副标题
 *
 */

#import "NewsTableCell.h"
#import "UIImageView+WebCache.h"
#import "UIImage+WebP.h"
#import "FashionNewsModel.h"

#import "UIImage+MultiFormat.h"
#import "UIImage+WebP.h"
#import "UIImage+GIF.h"

@interface NewsTableCell()
@property (nonatomic,retain) UIImageView * logoImageView; // logo图片
@property (nonatomic,retain) UIImageView * thumbImageView; // 标题图片


@property (nonatomic,retain) CAGradientLayer * gradient;
@end

@implementation NewsTableCell
- (void)dealloc
{
    [_titleLabel release];
    [_logoImageView release];
    [_thumbImageView release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;
}
-(void)createViews{
    self.backgroundColor = [UIColor clearColor];
    self.thumbImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_thumbImageView];
    
    self.titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont boldSystemFontOfSize:21];
    _titleLabel.numberOfLines = 2;
    _titleLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    
    self.logoImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_logoImageView];
    [_logoImageView release];
    
    self.gradient = [CAGradientLayer layer];
    _gradient.colors = [NSArray arrayWithObjects:(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:0].CGColor,
                       (id)[UIColor colorWithRed:88/255.0 green:81/255.0 blue:79/255.0 alpha:0.5].CGColor,
                       (id)[UIColor colorWithRed:28/255.0 green:26/255.0 blue:24/255.0 alpha:0.8].CGColor,nil];
    [_thumbImageView.layer insertSublayer:_gradient atIndex:0];
    [_thumbImageView release];
    
}
-(void)layoutSubviews{
    _thumbImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-1);
    _titleLabel.frame = CGRectMake(70, _thumbImageView.frame.size.height - 65, self.frame.size.width-70, 60);
    _logoImageView.frame = CGRectMake(10, self.frame.size.height-65, 50, 50);
    
    _gradient.frame = CGRectMake(0, _thumbImageView.frame.size.height-80, self.frame.size.width, 80);

}
-(void)sendMessageWithModel:(FashionNewsModel *)model{
    
    // 当图片下载成功后切割图片
    [_thumbImageView sd_setImageWithURL:[NSURL URLWithString:model.thumb] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        self.originalImage = _thumbImageView.image; // 首先传过去原始的image
        
#if 0
        // 方法一：压缩图片并裁剪。会导致图片变的不清晰
        
        // 压缩图片大小
        // 创建一个context，设定图片要压缩为多大
        UIGraphicsBeginImageContext(CGSizeMake(_thumbImageView.frame.size.width, 300));
        // 绘制图片大小
        [_thumbImageView.image drawInRect:CGRectMake(0, 0, _thumbImageView.frame.size.width, 300)];;
        // 从当前context中创建一个改变大小后的图片
        UIImage * endImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        // 切割图片
        CGImageRef imageRef = CGImageCreateWithImageInRect(endImage.CGImage, CGRectMake(0, 0, _thumbImageView.frame.size.width, 170));
        [_thumbImageView setImage:[UIImage imageWithCGImage:imageRef]];
#endif
        
#if 1
        // 方法二：按照窗口宽高比例，将原图横向或者纵向裁剪掉多余的部分
        CGSize newSize;
        CGImageRef imageRef = nil;
        if((_originalImage.size.width / _originalImage.size.height) < (_thumbImageView.frame.size.width / _thumbImageView.frame.size.height) ){
            newSize.width = _originalImage.size.width;
            newSize.height = _originalImage.size.width*_thumbImageView.frame.size.height/_thumbImageView.frame.size.width;
//            imageRef = CGImageCreateWithImageInRect([_originalImage CGImage], CGRectMake(0, fabs(_originalImage.size.height-newSize.height)/2, newSize.width, newSize.height)); // 显示图片的中央
            imageRef = CGImageCreateWithImageInRect([_originalImage CGImage], CGRectMake(0, 0, newSize.width, newSize.height)); // 显示图片的最上方
        }else{
            newSize.height = _originalImage.size.height;
            newSize.width = _originalImage.size.height*_thumbImageView.frame.size.width/_thumbImageView.frame.size.height;
            imageRef = CGImageCreateWithImageInRect([_originalImage CGImage], CGRectMake(0, 0, newSize.width, newSize.height));
        }
        _thumbImageView.image = [UIImage imageWithCGImage:imageRef];
#endif
    }];
    [_logoImageView sd_setImageWithURL:[NSURL URLWithString:model.cat_logo]];
    _titleLabel.text = model.title;
}

@end
